package com.ruix.app.util;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;

/**
 * Created by hadexs on 2017/5/11.
 */

public class EMMessageUtil {


    ////注册失败会抛出HyphenateException

    //创建账户
    public static void acreateAccount(String username,String pwd) throws HyphenateException {
        EMClient.getInstance().createAccount(username, pwd); ////同步方法
    }

    //登录成功后需要调用
    public static void login(String userName,String password,EMCallBack callBack){
        EMClient.getInstance().login(userName,password,callBack);
    }




    public static void sendGroupMessage(String content, String toChatUsername) {
        //创建一条文本消息，content为消息文字内容，toChatUsername为对方用户或者群聊的id，后文皆是如此
        EMMessage message = EMMessage.createTxtSendMessage(content, toChatUsername);
        //如果是群聊，设置chattype，默认是单聊
        //        if (chatType == CHATTYPE_GROUP)
        message.setChatType(EMMessage.ChatType.GroupChat);
        //发送消息
        EMClient.getInstance().chatManager().sendMessage(message);

    }

    public static void sendAudioMessage(String filePath, int lenght, String toChatUsername) {
        //filePath为语音文件路径，length为录音时间(秒)
        EMMessage message = EMMessage.createVoiceSendMessage(filePath, lenght, toChatUsername);
        //如果是群聊，设置chattype，默认是单聊
        //        if (chatType == CHATTYPE_GROUP)
        message.setChatType(EMMessage.ChatType.GroupChat);
        EMClient.getInstance().chatManager().sendMessage(message);
    }

    //发送图片
    public static void sendImage(String imagePath, boolean originPic, String toChatUsername) {
        //imagePath为图片本地路径，false为不发送原图（默认超过100k的图片会压缩后发给对方），需要发送原图传true
        EMMessage message = EMMessage.createImageSendMessage(imagePath, originPic, toChatUsername);
        //如果是群聊，设置chattype，默认是单聊
//        if (chatType == CHATTYPE_GROUP)
        message.setChatType(EMMessage.ChatType.GroupChat);
        EMClient.getInstance().chatManager().sendMessage(message);
    }

    //屏蔽群消息
    public static void blockGroupMessage(String groupId) throws HyphenateException {
        EMClient.getInstance().groupManager().blockGroupMessage(groupId);//需异步处理
    }

    //解除屏蔽群
    public static void unblockGroupMessage(String groupId) throws HyphenateException {
        EMClient.getInstance().groupManager().unblockGroupMessage(groupId);//需异步处理
    }



//    //屏蔽群消息
//    public void muteGroupMembers(String groupId, List<String> muteMembers) throws HyphenateException {
//        EMClient.getInstance().groupManager().muteGroupMembers(groupId, muteMembers,Long.MAX_VALUE);//需异步处理
//    }
//
//    //解除屏蔽群
//    public void unMuteGroupMembers(String groupId, List<String> muteMembers) throws HyphenateException {
//        EMClient.getInstance().groupManager().unMuteGroupMembers(groupId, muteMembers);//需异步处理
//    }

    //
    public void fetchGroupMuteList(String groupId, int pageNum, int pageSize) throws HyphenateException {
        EMClient.getInstance().groupManager().fetchGroupMuteList(groupId, pageNum, pageSize);
    }


    public static List<EMMessage>  loadMessage(String username){
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username);
//获取此会话的所有消息
        if (conversation != null) {
            List<EMMessage> messages = conversation.getAllMessages();
            return messages;
        }
        return null;
//SDK初始化加载的聊天记录为20条，到顶时需要去DB里获取更多
//获取startMsgId之前的pagesize条消息，此方法获取的messages SDK会自动存入到此会话中，APP中无需再次把获取到的messages添加到会话中
//        List<EMMessage> messages = conversation.loadMoreMsgFromDB(startMsgId, 20);
    }

    public static List<EMMessage>  loadMessage(String username,String startMsgId,int page) {
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username);
        return conversation.loadMoreMsgFromDB(startMsgId, 20);
    }




}
